/**
 * 定制表单
 */
import jQuery  from 'jquery'
import { useEffect } from 'react';
import { useForm } from '../../framework/common/context/metadata-context';
import eventBus from '../../framework/common/eventBus';
import message from '../../framework/common/utils/message';
import "./jquery.cf.js"
import useWatermark from '../hooks/useWatermark';
import "./../index.css";

export default function(){

    const form = useForm();
    const { data: tplData, urlPrefix, params } = useForm().root;

    //只有流程发起前才应用默认值配置
    var ff_data = tplData.form.form_data;

    var flowStatus;
    if(!tplData.ProcessInfo || !tplData.ProcessInfo.ProcessGuid){
        flowStatus = jQuery.flowStatus.start;
    } 
    if(tplData.ProcessInfo && tplData.ProcessInfo.ProcessStatus){
        if(tplData.ProcessInfo.ProcessStatus == -3) flowStatus = jQuery.flowStatus.draft;
        if(tplData.ProcessInfo.ProcessStatus == -4)  flowStatus = jQuery.flowStatus.start;
    }
    if(tplData.StepInfo && tplData.StepInfo.step_type && tplData.StepInfo.step_type == 1 && tplData.form_readonly == false){
        if(tplData.ProcessInfo && (tplData.ProcessInfo.ProcessStatus == 0)){
            flowStatus = jQuery.flowStatus.restart;
        }
    }

    var formOptions = null;
    //tplData.form.form_custom.url_pc = tplData.form.form_custom.url_pc.replace("http://10.20.21.21:8881", "http://localhost:8881");//外挂表单测试代码，更换外挂表单域名
    formOptions = {
        el: "#form-runtime",
        flow_status: flowStatus,
        readonly:tplData.form_readonly,
        viewMode:tplData.view_mode,
        package_id:params.package_id,
        business_id:tplData.ProcessInfo.BusinessGuid,
        process_id:tplData.ProcessInfo.ProcessGuid,
        server_address:tplData.ServerAddr,
        ff_data:ff_data,
        ori_data:tplData,
        cf_setting:tplData.form.form_custom,
        user_name:tplData.CurUserName,
        user_id:tplData.CurUserGUID,
        ffDataChanged:function(ff_data){
            params.form_data = ff_data;
        },
        ffFileSort:function(fileSort){
            params.file_sort = fileSort;
        },
        ffTextChanged:function(ff_text){
            //refreshTitle(ff_text);
            eventBus.publish("refreshTitle.update", {
                form_data: tplData.form.form_data, 
                params: params,
                tplData: tplData
            })
        },
        ffErro:function(erroMsg){ 
            if(typeof erroMsg === "object"){
                message.info({
                    title: erroMsg?.title || "",
                    content: erroMsg?.text || ""
                });
            }else{
                message.info({
                    content: erroMsg
                });
            }
        },
        resize:function(){
            console.log("resize")
            // $(".mask_div").empty();
            // if (tplData.watermark.isShow == '1') {
            //     setTimeout(function () {
            //         watermark({watermark_txt:tplData.watermark.content,watermark_width:178.3,watermark_height:66.6},$('#main-content'));
            //     });
            // }
        }
    };
    useEffect(function(){
        jQuery.cf(formOptions);
        params.form_data = ff_data;
    },[])

    window.__watermarkContent__ = form.data.watermark.content
    useWatermark(".runtime-form");//水印

    return <>
        <div id="form-runtime" className='form-runtime'></div>
    </>
}